package com.aistar.service.impl;

import com.aistar.mapper.MemberMapper;
import com.aistar.pojo.Member;
import com.aistar.pojo.MemberExample;
import com.aistar.service.MemberService;
import com.aistar.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MemberServiceImpl implements MemberService {



    @Override
    public boolean save(Member member) {// member 只有手机号
        //手机号码 已经 注册过
       if( this.getByTelno1(member.getMemberTelno())){
           return false;
       }else{ //没有注册过

           SqlSession sqlSession =  MyBatisUtil.getSqlSession();
           int rows = sqlSession.getMapper(MemberMapper.class).insert(member);
           sqlSession.commit();
           MyBatisUtil.closeSqlSession();
           return rows >0;
       }


    }

    @Override
    public boolean update(Member member) {
        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        int rows = sqlSession.getMapper(MemberMapper.class).updateByPrimaryKey(member);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession();
        return rows >0;
    }

    @Override
    public Member getById(int memberId) {
        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        Member member =  mapper.selectByPrimaryKey(memberId);
        MyBatisUtil.closeSqlSession();
        return member;
    }

    @Override
    public Member getByTelno(Long memberTelno) {
        Member member = null;
        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberTelnoEqualTo(memberTelno);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = mapper.selectByExample(example);//select * from member where telno = ? and status =0

        if(memberList!=null)
            member = memberList.get(0);


        MyBatisUtil.closeSqlSession();
        return member;
    }

    @Override
    public Member login(String username, String password) {
        Member member = null;
        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberNameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = mapper.selectByExample(example);          //select * from member where username = ? and password = ? and status =0

        if(memberList!=null)
            member = memberList.get(0);
        return member;
    }

    @Override
    public Member loginByTelno(Long memberTelno, String password) {
        Member member = null;
        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberTelnoEqualTo(memberTelno);
        criteria.andPasswordEqualTo(password);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = mapper.selectByExample(example);          //select * from member where username = ? and password = ? and status =0

        if(memberList!=null)
            member = memberList.get(0);
        return member;

    }




    public Boolean getByTelno1(Long memberTelno) {
        Member member = null;
        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberTelnoEqualTo(memberTelno);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = mapper.selectByExample(example);//select * from member where telno = ? and status =0

        if(memberList!=null){
            MyBatisUtil.closeSqlSession();
            return false;}
        else {
            MyBatisUtil.closeSqlSession();
            return true;
        }


    }

    @Override
    public List<Member> selectall() {

        SqlSession sqlSession =  MyBatisUtil.getSqlSession();
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
       List<Member> memberList =  mapper.selectByExample(null);
        MyBatisUtil.closeSqlSession();
        return memberList;

    }
}
